package JDBCUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class increase extends JFrame {
    private static final long serialVersionUID = -1928970409928880648L;
    JLabel jlpno = new JLabel("奖惩    编号：");
    JLabel jlname = new JLabel("奖惩   类型：");
    JLabel jlaward = new JLabel("奖惩   金额：");
    JLabel jlreason = new JLabel("奖惩   缘由：");

    JTextField jtpno = new JTextField("",20);
    JTextField jtname = new JTextField("",20);
    JTextField jtaward = new JTextField("",20);
    JTextField jtreason = new JTextField("",20);

    JButton buttonadd = new JButton("添加");
    JButton buttonreturn = new JButton("返回");

    public increase() {


        JPanel jppno = new JPanel();
        JPanel jpname = new JPanel();
        JPanel jpaward = new JPanel();
        JPanel jpreason= new JPanel();


        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        jppno.add(jlpno);
        jppno.add(jtpno);

        jpname.add(jlname);
        jpname.add(jtname);

        jpaward.add(jlaward);
        jpaward.add(jtaward);

        jpreason.add(jlreason);
        jpreason.add(jtreason);


        jpforbutton.add(buttonadd);
        jpforbutton.add(buttonreturn);


        buttonadd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                    boolean flag =false;
                    flag= !jtname.getText().equals("") && !jtreason.getText().equals("");

                System.out.println(flag);


                if ( !flag) {

                    if(e.getSource()==buttonadd){
                        javax.swing.JOptionPane.showMessageDialog(null, "添加格式有误！！！重新添加！添加操作结束!");
                    }

                } else {

                    Connection conn = null;
                    PreparedStatement ps=null;

                    String xname = jtname.getText();
                    String xpno = jtpno.getText();
                    String xaward = jtaward.getText();
                    String xreason = jtreason.getText();


                    String sql = "INSERT INTO staff(pno,name,award,reason) "
                            + "values(?,?,?,?)";

                    try{
                        Class.forName("com.mysql.cj.jdbc.Driver");
                        System.out.println("JBDC 加载成功!");
                    }catch(Exception a){
                        System.out.println("JBDC 语句结束!");
                        a.printStackTrace();
                    }
                    try{
                        conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/work","root","123456");

                        ps=conn.prepareStatement(sql);

                        ps.setString(3,xaward);
                        ps.setString(1,xpno);
                        ps.setString(2,xname);
                        ps.setString(4,xreason);
                        ps.executeUpdate();
                        if(e.getSource()==buttonadd){
                            javax.swing.JOptionPane.showMessageDialog(null, "添加成功!");
                        }

                    }catch (SQLException b){
                        System.out.println("添加失败！！！重新添加！");
                        if(e.getSource()==buttonadd){
                            javax.swing.JOptionPane.showMessageDialog(null, "添加失败！！！重新添加！添加操作结束!");
                        }
                    }finally{

                        try{
                            conn.close();
                            System.out.println("MySQL 关闭成功");
                        }catch (SQLException c){
                            System.out.println("MySQL 关闭失败 ");
                            c.printStackTrace();
                        }
                    }


                }

            }
        });

        buttonreturn.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                dispose();
            }

        });





        this.setTitle("添加教师信息");
        this.setLayout(new GridLayout(9,1));
        this.add(jppno);
        this.add(jpname);
        this.add(jpaward);
        this.add(jpreason);

        this.add(jpforbutton);
        this.setLocation(800,300);
        this.setSize(350,300);
        this.setVisible(true);
    }
}
